package com.ywjh.inter.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.ywjh.inter.conn.DBConnection;
import com.ywjh.inter.dao.EquBindDAO;
import com.ywjh.inter.entity.EquBindEntity;

public class EquBindService {

	private static EquBindService instance;
	
	public static final int TYPE_BGLU=1;
	public static final int TYPE_BPRE=2;
	
	private EquBindDAO equBingDAO;

	public static EquBindService getInstance() {
		if (instance == null) {
			instance = new EquBindService();
			instance.equBingDAO = new EquBindDAO();
		}
		return instance;
	}

	public boolean insert(EquBindEntity entity) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			int id = equBingDAO.insert(conn, entity);
			if (id > 0) {
				b = true;
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;
	}

	public boolean update(EquBindEntity entity) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			b = equBingDAO.update(conn, entity);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;
	}

	public List<EquBindEntity> list(String sn, int type) {
		List<EquBindEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = equBingDAO.list(conn, sn, type);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}

		return list;
	}

	public EquBindEntity find(int user_id, String sn, int type) {
		EquBindEntity entity = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			entity = equBingDAO.find(conn, user_id, sn, type);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return entity;
	}

	public List<EquBindEntity> listByUserId(int user_id) {
		List<EquBindEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = equBingDAO.listByUserId(conn, user_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}
	
	public List<EquBindEntity> listByUserIdAndType(int user_id, int type) {
		List<EquBindEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = equBingDAO.listByUserIdAndType(conn, user_id, type);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public List<EquBindEntity> listByTime(String start,String end , String users) {
		List<EquBindEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = equBingDAO.listByTime(conn, start,end,users);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}
	

}
